var TownLayer = cc.Layer.extend({
    initTown:function(townName){
        this.townName = townName;
        var townConfig = g_ClientConfig.town[townName];
        this.backgroundSpirte = cc.Sprite.create(townConfig.background);
        this.backgroundSpirte.setAnchorPoint(cc.p(0,0));
        this.backgroundSpirte.setPosition(cc.p(0,0));
        this.addChild(this.backgroundSpirte);
        this.frameSprite = cc.Sprite.create(townConfig.frame);
        this.frameSprite.setAnchorPoint(cc.p(0,0));
        this.frameSprite.setPosition(cc.p(0,0));
        this.backgroundSpirte.addChild(this.frameSprite);
        this.frameCastleSprite = cc.Sprite.create(townConfig.frame_castle);
        var backgroundRect = this.backgroundSpirte.getTextureRect();
        var frameCastleSpriteRect = this.frameCastleSprite.getTextureRect();
        this.frameCastleSprite.setAnchorPoint(cc.p(0,0));
        this.frameCastleSprite.setPosition(cc.p(backgroundRect.size.width - frameCastleSpriteRect.size.width,0));
        this.backgroundSpirte.addChild(this.frameCastleSprite);
        this.wushuguanSprite = new BuildingSpirte(townConfig.wushuguan);
        this.wushuguanSprite.setPosition(cc.p(townConfig.wushuguan_position[0],townConfig.wushuguan_position[1]));
        this.wushuguanSprite.setAnchorPoint(cc.p(0,0));
        this.wushuguanSprite.hide();
        this.backgroundSpirte.addChild(this.wushuguanSprite);
        this.initHumans();
    },
    initHumans:function(){
        var townConfig = g_ClientConfig.town[this.townName];
        for(var i in townConfig.stand_human){
            var human = townConfig.stand_human[i];
            var walk = Animation.createByName(human.animation);
            var walkSprite = walk.getSprite();
            walkSprite.setAnchorPoint(cc.p(0,0));
            walkSprite.setPosition(human.position[0],human.position[1]);
            walk.play(-1);
            this.backgroundSpirte.addChild(walkSprite);
        }
    },
});

var TownScene = cc.Scene.extend({
    onEnter:function(){
        this._super();
        UISystem.hideAll();
        var layer = new TownLayer();
        layer.initTown("qingzhou");
        this.addChild(layer);
        var ui = UISystem.selectUI("TownUI");
        ui.onwushuguan(function(){
            UISystem.showDialog("PKDialog");
            app.getSession().request(new GetAllBattlePlayerRequest(),GetAllBattlePlayerCB);
        });
    }
});